extends MarginContainer

@export var inventory_back : C_Inventory

const INV_C_STOWAGE = preload("res://Inventory/inv_c_stowage.tscn")

@onready var objects = %Objects
@onready var label = $MarginContainer/VBoxContainer/MarginContainer2/HBoxContainer/MarginContainer3/Label


# Called when the node enters the scene tree for the first time.
func _ready():
	if inventory_back:
		inventory_back.item_changed.connect(update_display_items)
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(_delta):
	if Input.is_action_just_pressed("items"):
		if visible:
			hide()
		else :
			show()
	pass


func update_display_items():
	var items = inventory_back.items
	for i in objects.get_children():
		i.queue_free()
	for item in items:
		var inv_c_stowage = INV_C_STOWAGE.instantiate()
		inv_c_stowage.selected.connect(mouse_click_select.bind(item))
		inv_c_stowage.choiced.connect(mouse_click_choiced.bind(item))
		objects.add_child(inv_c_stowage)
		inv_c_stowage.item = item


func mouse_click_select(item:Item):
	label.text = item.info
	update_display_items()

func mouse_click_choiced(item:Item):
	label.text = item.info


func _on_button_button_down():
	hide()
	pass # Replace with function body.


func open_bag() -> void:
	show()
	pass # Replace with function body.
